package enterprise.manage.security.utils;

import enterprise.manage.util.Constants;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * .
 * User: libofeng
 * Date: 2007-4-25
 * Time: 11:00:29
 * Description:IP处理辅助类
 */
public class IpUtil {

    /**
     * 构造函数，允许继承
     */
    protected IpUtil() {
    }

    /**
     * 返回图片设置的cookie ip,如果cookie不存在,则返回 request.getRemoteAddr().
     *
     * @param request HttpServletRequest
     * @return ip串
     */
    public static String getCookieIp(final HttpServletRequest request) {
        for (Cookie cookie : request.getCookies()) {
            if (cookie.getName().equals(Constants.IP_IMAGE_COOKIE_KEY)) {
                return cookie.getValue();
            }
        }

        return request.getRemoteAddr();
    }
}
